//
//  YHY_MyViewController.m
//  YHYRealx
//
//  Created by yanghuiyan on 16/5/3.
//  Copyright © 2016年 yanghuiyan. All rights reserved.
//

#import "YHY_MyViewController.h"
#import "YHY_MyTableViewCell.h"
#import "YHY_Setting_VC.h"
#import "FavoriteViewController.h"
@interface YHY_MyViewController ()<UITableViewDelegate, UITableViewDataSource, UINavigationControllerDelegate, UIImagePickerControllerDelegate>

@property (nonatomic, retain) YHYBaseTableView *myTabelView;

@property (nonatomic, retain) UIImageView *pic;

@property (nonatomic, retain) NSString *path;

@end


@implementation YHY_MyViewController

- (void)dealloc {
    
//    _myTabelView.delegate = nil;
//    _myTabelView.dataSource = nil;
    [_myTabelView release];
    [super dealloc];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    [self creatView];
    [self creatTableView];
}

#pragma mark - ------------ 创建视图 ---------------
- (void)creatView {
    
    //自定义标题视图
    UILabel *titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 200, 40)];
    titleLabel.text = @"我的";
    titleLabel.font = [UIFont boldSystemFontOfSize:22];
    titleLabel.textColor = [UIColor grayColor];
    titleLabel.textAlignment = NSTextAlignmentCenter;
    self.navigationItem.titleView = titleLabel;
    [titleLabel release];
    
    YHYBaseView *topView = [[YHYBaseView alloc] initWithFrame:CGRectMake(0, 0, WIDTH, HEIGHT * 0.3)];

    topView.backgroundColor = [UIColor yellowColor];
    
    self.pic = [[UIImageView alloc] init];
    _pic.userInteractionEnabled = YES;
    
    [topView addSubview:_pic];
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] init];
    [tap addTarget:self action:@selector(tapAction)];
    _pic.backgroundColor = [UIColor blueColor];
    [_pic addGestureRecognizer:tap];
//    [_pic setImage:[UIImage imageNamed:@"取消"]];
    
    NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
    
    if ([userDefaults dataForKey:@"img"]) {
        
        [_pic setImage:[UIImage imageWithData:[userDefaults dataForKey:@"img"]]];
        
    }
    
    [self.view addSubview:topView];
    [_pic mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.centerX.mas_equalTo(topView.mas_centerX);
        make.centerY.mas_equalTo(topView.mas_centerY);
        make.size.equalTo(@(100));
        
    }];
    _pic.layer.cornerRadius = 100 / 2;
    _pic.layer.masksToBounds = YES;
    
    // 创建TableView
//    self.myTabelView = [[YHYBaseTableView alloc] initWithFrame:CGRectMake(0, HEIGHT * 0.3, WIDTH, HEIGHT * 0.7 - 114) style:UITableViewStylePlain];
//    [_myTabelView registerClass:[YHY_MyTableViewCell class] forCellReuseIdentifier:@"movie"];
//    _myTabelView.delegate = self;
//    _myTabelView.dataSource = self;
//    
//    [self.view addSubview:_myTabelView];
//
//    [self creatTableView];
}

- (void)tapAction {
    
    UIImagePickerController *pickerC = [[UIImagePickerController alloc] init];
    pickerC.delegate = self;
    pickerC.allowsEditing = YES;
    [self presentViewController:pickerC animated:YES completion:^{
        
    }];
    
}

#pragma mark - ------------ 编辑完成照片会走的方法 ---------------
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary<NSString *,id> *)info {
    
    /**< 保存编辑图片 */
    UIImage *img = [info objectForKey:UIImagePickerControllerEditedImage];
    
    if (img != nil) {
        
        _pic.image = img;
        
    }
    
    /**< 存储图片 */
    NSData *data = [[NSData alloc] init];
    if (UIImagePNGRepresentation(img)) {
        data =UIImagePNGRepresentation(img);
    }else {
        data = UIImageJPEGRepresentation(img, 1.0);
    }
    
    
    [[NSFileManager defaultManager] createFileAtPath:_path contents:data attributes:nil];
    NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
    [userDefaults setObject:data forKey:@"img"];
    [self dismissViewControllerAnimated:YES completion:^{
        
    }];
    
}

#pragma mark - ------------ 创建tableView ---------------
- (void) creatTableView {
    
    self.myTabelView = [[YHYBaseTableView alloc] initWithFrame:CGRectMake(0, HEIGHT * 0.3, WIDTH, HEIGHT * 0.7) style:UITableViewStylePlain];
    [self.myTabelView registerClass:[YHY_MyTableViewCell class] forCellReuseIdentifier:@"TableViewCellIdentifier"];
    self.myTabelView.delegate = self;
    self.myTabelView.dataSource = self;
    [self.view addSubview:self.myTabelView];
    
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 50;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return 2;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.row == 0) {
        YHY_MyTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"TableViewCellIdentifier"];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        cell.label.text = @"设置 >";
        return cell;

    }
    YHY_MyTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"TableViewCellIdentifier"];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    cell.label.text = @"收藏 >";
    return cell;
    
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.row == 0) {
        
        YHY_Setting_VC  *VC = [[YHY_Setting_VC alloc] init];
        [self.navigationController pushViewController:VC animated:YES];
        
    }
    if (indexPath.row == 1) {
        
        FavoriteViewController *VC = [[FavoriteViewController alloc] init];
        [self.navigationController pushViewController:VC animated:YES];
    }
    
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
